package com.atguigu.ext;

import com.atguigu.bean.Car;
import com.atguigu.bean.Color;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.stereotype.Component;

/**
 * bean的定义信息
 * @author gcq
 * @Create 2021-02-08
 * BeanDefinitionRegistryPostProcessor 继承了 BeanFactoryPostProcessor接口
 * 同时自己添加了一个新的方法 postProcessBeanDefinitionRegistry 用于管理bean的定义信息
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    /**
     *
     * @param registry
     * @throws BeansException
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("postProcessBeanDefinitionRegistry..bean的数量：" + registry.getBeanDefinitionCount());
//        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
        // 构建器生成bean
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Car.class).getBeanDefinition();
        // 注册，加到容器中
        registry.registerBeanDefinition("car",beanDefinition);
    }

    /**
     * BeanFactoryPostProcessor的接口
     * @param beanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor...bean的数量:" + beanFactory.getBeanDefinitionCount());
    }
}